﻿package com.wandian.view.mediator
{
	import com.greensock.TweenLite;
	import com.wandian.controller.*;
	import com.wandian.data.GameData;
	import com.wandian.model.*;
	import com.wandian.model.vo.*;
	import com.wandian.view.components.ui.Gift;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.*;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class GameGiftMediator extends Mediator
	{
		private var arrXY:Array;
		private var arrShop:Array;
		private var _global:GameData;
		private var _inRoomData:InRoomPlayerDataProxy;
		private var _accountProxy:AccountProxy;
		public static const NAME:String="GameGiftMediator";

		public function GameGiftMediator(viewComponent:Object=null):void
		{
			this._global=GameData.getInstance();
			super(NAME, viewComponent);
			this._inRoomData=facade.retrieveProxy(InRoomPlayerDataProxy.NAME) as InRoomPlayerDataProxy;
			this._accountProxy=facade.retrieveProxy(AccountProxy.NAME) as AccountProxy;
			return;
		}

		override public function onRegister():void
		{
			this.init();
			return;
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.GIVE_GIFT_RES, CLZNoticeName.STAND_UP_RSP, CLZNoticeName.SITDOWN_RSP, CLZNoticeName.GIFT_CANCEL_RES, CLZNoticeName.USE_PROP_RES];
		}

		override public function handleNotification(notification:INotification):void
		{
			var data:Object=null;
			var giftPlayerVo:PlayerVo=null;
			var toPlayerVo:PlayerVo=null;
			var playerVo:PlayerVo=null;
			var _loc_6:uint=0;
			var _loc_7:PlayerVo=null;
			switch (notification.getName())
			{
				case CLZNoticeName.GIVE_GIFT_RES:
				{
					data=notification.getBody();
					giftPlayerVo=this._inRoomData.getInRoomPlayer(data.userid) as PlayerVo;
					giftPlayerVo.money=data.money;
					if (giftPlayerVo.userid == this._global.userInfo.userVo.userid)
					{
						this._global.userInfo.money=data.money;
					}
					toPlayerVo=this._inRoomData.getInRoomPlayer(data.toMid) as PlayerVo;
					if (toPlayerVo == null)
					{
						return;
					}
					if (toPlayerVo.userid == this._global.userInfo.userVo.userid)
					{
						if (data.pcate <= 10)
						{
							this._accountProxy.addProp(data.pcate, data.pframe, data.num);
							trace("成功");
							return;
						}
					}
					toPlayerVo.giftPcate=data.pcate;
					toPlayerVo.giftPframe=data.pframe;
					this.moveGift(giftPlayerVo, toPlayerVo, "", data.pcate, data.pframe);
					break;
				}
				case CLZNoticeName.SITDOWN_RSP:
				{
					playerVo=notification.getBody() as PlayerVo;
					this.showGift(playerVo.seatId, playerVo.giftPcate, playerVo.giftPframe);
					break;
				}
				case CLZNoticeName.STAND_UP_RSP:
				{
					data=notification.getBody();
					this.removeSitGift(data.seatId);
					break;
				}
				case CLZNoticeName.GIFT_CANCEL_RES:
				{
					_loc_6=notification.getBody() as uint;
					_loc_7=this._inRoomData.getSitPlayerByMid(_loc_6) as PlayerVo;
					_loc_7.giftPcate=0;
					_loc_7.giftPframe=0;
					this.showGift(_loc_7.seatId, 0, 0);
					break;
				}
				case CLZNoticeName.USE_PROP_RES:
				{
					data=notification.getBody();
					if (data.pcate >= GameData.PROP_PCATE_ID_GIFT)
					{
						if (data.useMid == this._global.userInfo.userVo.userid)
						{
							this._global.userInfo.money=data.money;
						}
						_loc_7=this._inRoomData.getSitPlayerByMid(data.toMid) as PlayerVo;
						_loc_7.giftPcate=data.pcate;
						_loc_7.giftPframe=data.pframe;
						this.showGift(_loc_7.seatId, data.pcate, data.pframe);
					}
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function init():void
		{
			var _loc_1:Array=[this.mcGift.gift1, this.mcGift.gift2, this.mcGift.gift3, this.mcGift.gift4, this.mcGift.gift5, this.mcGift.gift6, this.mcGift.gift7, this.mcGift.gift8, this.mcGift.gift9];
			this.arrXY=new Array(9);
			this.arrShop=[this.mcGift.shop1, this.mcGift.shop2, this.mcGift.shop3, this.mcGift.shop4, this.mcGift.shop5, this.mcGift.shop6, this.mcGift.shop7, this.mcGift.shop8, this.mcGift.shop9];
			var _loc_2:int=0;
			while (_loc_2 < this.arrShop.length)
			{

				this.arrXY[_loc_2]=new Gift(_loc_1[_loc_2]);
				this.arrShop[_loc_2].addEventListener(MouseEvent.CLICK, this.clickShopHandler);
				this.arrShop[_loc_2].visible=false;
				this.arrXY[_loc_2].mcGift.addEventListener(MouseEvent.CLICK, this.clickGiftHandler);
				this.arrXY[_loc_2].mcGift.id=_loc_2;
				_loc_2=_loc_2 + 1;
			}
			return;
		}

		/**
		 * 若当前玩家坐下，当有玩家坐下，显示赠送按钮
		 * @param seatId
		 * @param giftPcate
		 * @param giftPframe
		 */
		public function showGift(seatId:int, giftPcate:int, giftPframe:int):void
		{
			this.arrXY[(seatId - 1)].showGift(giftPcate, giftPframe);
			if (giftPcate == 0)
			{
			}
			if (giftPframe == 0)
			{
			}
			if (this._global.seatId != 0)
			{
				this.arrShop[(seatId - 1)].visible=true;
				this.arrShop[(seatId - 1)].addEventListener(MouseEvent.CLICK, this.clickShopHandler);
			}
			else
			{
				this.arrShop[(seatId - 1)].visible=false;
				this.arrXY[(seatId - 1)].mcGift.addEventListener(MouseEvent.CLICK, this.clickGiftHandler);
			}
			return;
		}

		private function clickGiftHandler(event:MouseEvent):void
		{
			var _loc_2:*=event.currentTarget.id + 1;
			var _loc_3:*=this._inRoomData.getSitPlayerBySeatId(_loc_2);
			if (this._global.seatId == 0)
			{
				this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.tip["BUYIN_GIFT_ERR_SIT"]});
				return;
			}
			if (_loc_3.userid == this._global.userInfo.userVo.userid)
			{
				this.sendNotification(CLZNoticeName.USEPROPS_BTN, _loc_3, "GIFT");
			}
			else
			{
				this.sendNotification(CLZNoticeName.SHOW_SHOPING_GIFT, _loc_3);
			}
			return;
		}

		public function moveGift(param1:PlayerVo, param2:PlayerVo, param3:String, param4:int, param5:int):void
		{
			var _loc_6:Gift=null;
			if (param1.userid == param2.userid)
			{
				this.showGift(param1.seatId, param4, param5);
			}
			else
			{
				_loc_6=new Gift();
				_loc_6.showGift(param4, param5);
				_loc_6.x=this.arrXY[(param1.seatId - 1)].mcGift.x;
				_loc_6.y=this.arrXY[(param1.seatId - 1)].mcGift.y;
				this.mcGift.addChild(_loc_6);
				TweenLite.to(_loc_6, 1, {x: this.arrXY[(param2.seatId - 1)].mcGift.x, y: this.arrXY[(param2.seatId - 1)].mcGift.y, onComplete: this.moveToFriendFinish, onCompleteParams: [_loc_6, param1, param2, param3, param4, param5]});
			}
			return;
		}

		private function moveToFriendFinish(param1:Gift, param2:PlayerVo, param3:PlayerVo, param4:String, param5:int, param6:int):void
		{
			var _loc_7:*=param1;
			this.arrXY[(param3.seatId - 1)].showGift(param5, param6);
			this.arrXY[(param3.seatId - 1)].mcGift.addEventListener(MouseEvent.CLICK, this.clickGiftHandler);
			if (param3.seatId == param2.seatId)
			{
				this.arrShop[(param3.seatId - 1)].visible=false;
				this.arrShop[(param3.seatId - 1)].removeEventListener(MouseEvent.CLICK, this.clickShopHandler);
			}
			if (_loc_7 != null)
			{
			}
			if (this.mcGift.contains(_loc_7))
			{
				this.mcGift.removeChild(_loc_7);
				_loc_7=null;
			}
			return;
		}

		public function removeSitGift(seatId:int, boolVisible:Boolean=false):void
		{
			this.arrXY[(seatId - 1)].removeGift();
			if (this.arrXY[(seatId - 1)].mcGift.hasEventListener(MouseEvent.CLICK))
			{
				this.arrXY[(seatId - 1)].mcGift.removeEventListener(MouseEvent.CLICK, this.clickGiftHandler);
			}
			var playerVo:*=this._inRoomData.getSitPlayerBySeatId(seatId);
			if (playerVo.userid == this._global.userInfo.userVo.userid)
			{
				if (this._global.seatId == seatId)
				{
//					if (!boolVisible)
//					{
//						this.arrShop[(seatId - 1)].visible=true;
//						this.arrShop[(seatId - 1)].addEventListener(MouseEvent.CLICK, this.clickShopHandler);
//					}
//					else
//					{
//						this.arrShop[(seatId - 1)].visible=false;
//						this.arrShop[(seatId - 1)].removeEventListener(MouseEvent.CLICK, this.clickShopHandler);
//					}
				}
			}
			this.arrShop[(seatId - 1)].visible=false;
			this.arrShop[(seatId - 1)].removeEventListener(MouseEvent.CLICK, this.clickShopHandler);


			return;
		}

		public function removeAllSit():void
		{
			var _loc_1:int=0;
			while (_loc_1 < this.arrXY.length)
			{

				this.removeSitGift((_loc_1 + 1));
				_loc_1=_loc_1 + 1;
			}
			return;
		}

		private function clickShopHandler(event:MouseEvent):void
		{
			var seatId:int=int(String(event.currentTarget.name).substring(4, 5));
			var playerVo:PlayerVo=this._inRoomData.getSitPlayerBySeatId(seatId);
			this.sendNotification(CLZNoticeName.SHOW_SHOPING_GIFT, playerVo);
			return;
		}

		private function get mcGift():MovieClip
		{
			return viewComponent as MovieClip;
		}

	}
}
